//
//  AppDelegate.swift
//  APPDemo
//
//  Created by 郭红旗 on 2021/10/12.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        ZTTXManager.start(withAccountId: "")
//        UIResponder.hookUIView()
        return true
    }

}

private var holderView:UIView?
private var holderColor:UIColor?


extension UIWindow{
    open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = super.hitTest(point, with: event)
        if holderView == nil  {
            holderColor = view?.backgroundColor
            view?.backgroundColor = UIColor.orange
            holderView?.backgroundColor = holderColor
            holderView = view

        }else if holderView != view{
            holderView?.backgroundColor = holderColor
            holderColor = view?.backgroundColor
            view?.backgroundColor = UIColor.orange
            holderView = view

        }else{
//            holderView?.backgroundColor = holderColor
        }
//        debugPrint(view)
        
        return view
    }
    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        return super.point(inside: point, with: event)
    }
}

extension UIResponder{
    
    static func hookUIView(){
//        guard let appearMethod = class_getInstanceMethod(UIApplication.self, #selector(touchesMoved(_:with:))),let hookMethod = class_getInstanceMethod(UIApplication.self, #selector(hook_touchesMoved(_:with:))) else {
//            return
//        }
        guard let appearMethod = class_getInstanceMethod(UIApplication.self, #selector(touchesMoved(_:with:))),let hookMethod = class_getInstanceMethod(UIApplication.self, #selector(hook_touchesMoved(_:with:))) else {
            return
        }
        method_exchangeImplementations(appearMethod, hookMethod)
    }
    
    @objc func hook_touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
//        debugPrint(event)
        hook_touchesMoved(touches, with: event)
    }

}

//extension UIApplication{
//    static func hookUIApplication(){
////        guard let appearMethod = class_getInstanceMethod(UIApplication.self, #selector(sendAction(_:to:from:for:))),let hookMethod = class_getInstanceMethod(UIApplication.self, #selector(hook_sendAction(_:to:from:for:))) else {
////            return
////        }
////        method_exchangeImplementations(appearMethod, hookMethod)
//        UIApplication.self.lbp_swizzleMethod(#selector(sendAction(_:to:from:for:)), swizzledSelector: #selector(hook_sendAction(_:to:from:for:)))
//    }
//    
//    @objc open func hook_sendAction(_ action: Selector, to target: Any?, from sender: Any?, for event: UIEvent?) -> Bool{
//        guard let targetClass = target.self as? AnyObject,let senderClass = sender.self as? AnyObject else {
//            return hook_sendAction(action, to: target, from: sender, for: event)
//        }
//        
//        guard let vc = nextresponsder(viewself: senderClass)else {
//            return hook_sendAction(action, to: target, from: sender, for: event)
//        }
//        
//        let targetName = NSStringFromClass(type(of: targetClass))
//        let senderName = NSStringFromClass(type(of: senderClass))
//        let actionName = NSStringFromSelector(action)
//        let viewName = NSStringFromClass(type(of: vc))
//
//        let actionDetailInfo = "\(targetName) - \(senderName) - \(actionName) - \(viewName)"
//        debugPrint(actionDetailInfo)
//        
//        
//        return hook_sendAction(action, to: target, from: sender, for: event)
//    }
//
//    func nextresponsder(viewself:Any)->UIViewController?{
//        guard var vc = viewself as? UIResponder else {
//            return nil
//        }
//     
//        while vc.isKind(of: UIViewController.self) != true {
//            vc = vc.next!
//        }
//        return vc as? UIViewController
//    }
//    
//}

extension UIViewController{

    static func hookUIViewController(){
        guard let appearMethod = class_getInstanceMethod(UIViewController.self, #selector(viewDidAppear(_:))),let hookMethod = class_getInstanceMethod(UIViewController.self, #selector(hook_ViewDidAppear(_:))) else {
            return
        }
        method_exchangeImplementations(appearMethod, hookMethod)
    }
    
    @objc func hook_ViewDidAppear(_ animated:Bool){
        let appearDetailInfo = "\(NSStringFromClass(type(of: self))) - didAppear"
        debugPrint(appearDetailInfo)
        hook_ViewDidAppear(animated)
    }
}

